from db_handler import MysqlHander
from my_http import MyHttp
import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import json

class StockBusiness:
    def __init__(self):
        self.db = MysqlHander("config.ini")
        self.file = open("business.txt", "w", encoding="utf8")
        self.business = []
    
    def get_data(self, tag, code):
        url = "https://emweb.securities.eastmoney.com/PC_HSF10/BusinessAnalysis/PageAjax?code=" + tag + code
        http_data = MyHttp.http_json_data(url)
        json_data = json.loads(http_data)
        if "status" in json_data:
            return
        for d in json_data["zygcfx"]:
            if d["MAINOP_TYPE"] == "2":
                self.business.append(d["ITEM_NAME"])
    
    def get_gupiao_business(self, sql):
        print(sql)
        gupiao_data = self.db.query_sql(sql)
        for gupiao in gupiao_data:
            if int(gupiao[0]) == 1:
                self.get_data("SH", gupiao[0])
            if int(gupiao[0]) == 2:
                self.get_data("SZ", gupiao[0])
        self.write_file_data()
    
    def write_file_data(self):
        business_map = {}
        #business_set = set(self.business)
        for s in self.business:
            if s in business_map:
                business_map[s] = business_map[s] + 1
            else:
                business_map[s] = 1
        for k,v in business_map.items():
            self.file.write(k + "\t" + str(v) + "\n")
        self.file.close()

if __name__ == '__main__':
    code = StockBusiness()
    code.get_gupiao_business("select Fcode,Fname,Fexchange_id from entity_gupiao;")